﻿using HtmlAgilityPack;
using jutehui.GetData;
using jutehui.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Filters;
using Winista.Text.HtmlParser.Lex;
using Winista.Text.HtmlParser.Util;

namespace GetData
{
    public static class CustomMethod
    {

        private static string proxyIP = "";
        #region list 转化成datatable
        /// <summary>
        /// 将得到的数组转换成datatable数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static DataTable ListToDataTable<T>(this IList<T> data, string tableName, ref string msg)
        {
            DataTable table = new DataTable(tableName);
            try
            {
                //special handling for value types and string
                if (typeof(T).IsValueType || typeof(T).Equals(typeof(string)))
                {

                    DataColumn dc = new DataColumn("Value");
                    table.Columns.Add(dc);
                    foreach (T item in data)
                    {
                        DataRow dr = table.NewRow();
                        dr[0] = item;
                        table.Rows.Add(dr);
                    }
                }
                else
                {
                    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
                    foreach (PropertyDescriptor prop in properties)
                    {
                        table.Columns.Add(prop.Name,
                        Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
                    }
                    foreach (T item in data)
                    {
                        DataRow row = table.NewRow();
                        foreach (PropertyDescriptor prop in properties)
                        {
                            try
                            {
                                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                            }
                            catch (Exception ex)
                            {
                                row[prop.Name] = DBNull.Value;
                            }
                        }
                        table.Rows.Add(row);
                    }
                }
                return table;
            }
            catch (Exception ex)
            {
                msg += "ListToDataTable" + ex.Message.ToString();
                WriteLog(msg);
                return null;
            }
        }
        #endregion
        #region 根据url获取html页面代码
        /// <summary>
        /// 根据url获取页面信息
        /// </summary>
        /// <param name="WebUrl"></param>
        /// <returns></returns>
        public static string GetWebHTML(string WebUrl)
        {
            //test
            string strUrl = WebUrl;
            Encoding encoding = System.Text.Encoding.Default;
            WebRequest request = WebRequest.Create(strUrl);
            // request.Timeout = 20000;
            //request.Headers.Set("Pragma", "no-cache");
            //request.Method = "POST"; 
            request.Credentials = CredentialCache.DefaultCredentials;
            string responseFromServer = "";
            string sException = null;
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response.StatusDescription.ToUpper() == "OK")
                {
                    switch (response.CharacterSet.ToLower())
                    {
                        case "gbk":
                            encoding = Encoding.GetEncoding("GBK");//貌似用GB2312就可以
                            break;
                        case "gb2312":
                            encoding = Encoding.GetEncoding("GB2312");
                            break;
                        case "utf-8":
                            encoding = Encoding.UTF8;
                            break;
                        case "big5":
                            encoding = Encoding.GetEncoding("Big5");
                            break;
                        case "iso-8859-1":
                            encoding = Encoding.UTF8;//ISO-8859-1的编码用UTF-8处理，致少优酷的是这种方法没有乱码
                            break;
                        default:
                            encoding = Encoding.UTF8;//如果分析不出来就用的UTF-8
                            break;
                    }
                    string item = "Lenght:" + response.ContentLength.ToString() + "<br>CharacterSet:" + response.CharacterSet + "<br>Headers:" + response.Headers + "<br>";
                    Stream dataStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(dataStream, encoding);
                    responseFromServer = reader.ReadToEnd();

                    reader.Close();
                    dataStream.Close();
                    response.Close();
                    return responseFromServer;
                }
                else
                {
                    return "Error";
                }
            }
            catch (WebException e)
            {
                sException = e.Message.ToString();
                return sException;
            }
        }


        /// <summary>
        /// 使用 httphelper类通过url获取html
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetHtmlByHttpHelper(string url)
        {
            try
            {
                //创建Httphelper对象
                HttpHelper http = new HttpHelper();
                //创建Httphelper参数对象
                HttpItem item = new HttpItem()
                {
                    URL = url,//URL     必需项    
                    Method = "get",//URL     可选项 默认为Get   
                    ContentType = "text/html",//返回类型    可选项有默认值   
                    // ProxyIp = iplist[index],
                    Allowautoredirect = true
                    //ContentType = "application/x-www-form-urlencoded",//返回类型    可选项有默认值   
                };
                //请求的返回值对象
                HttpResult result = http.GetHtml(item);
                //获取请请求的Html
                string html = result.Html;
                return html;

            }
            catch (Exception ex)
            {
                WriteLog("GetHtmlByHttpHelper " + ex.Message.ToString() + ":" + url);
                return "";
            }
        }
        /// <summary>
        /// 使用代理url获取html
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetProxyByHttpHelper(string url)
        {
            try
            {
                //创建Httphelper对象
                HttpHelper http = new HttpHelper();
                //创建Httphelper参数对象
                HttpItem item = new HttpItem()
                {
                    URL = url,//URL     必需项    
                    Method = "get",//URL     可选项 默认为Get   
                    ContentType = "text/html",//返回类型    可选项有默认值   
                    //ProxyIp = proxy,
                    Allowautoredirect = true
                    //ContentType = "application/x-www-form-urlencoded",//返回类型    可选项有默认值   
                };


                //请求的返回值对象
                HttpResult result = http.GetHtml(item);
                //获取请请求的Html
                string html = result.Html;

                //获取请求的Cookie
                string cookie = result.Cookie;
                //获取URL重定向时最后一次访问的URL
                //  string ResponseUri = result.Header.ToString(); 
                return html;
            }
            catch (Exception ex)
            {
                WriteLog("GetHtmlByHttpHelper " + ex.Message.ToString() + ":" + url);
                return "";
            }
        }


        /// <summary>
        /// 根据没得比页面更新时间字符串
        /// </summary>
        /// <param name="timeStr"></param>
        /// <returns></returns>
        public static int timeStringToSecond(string timeStr)
        {
            int sumSecond = 0;
            int minIndex = 0;
            int secIndex = 0;
            if (timeStr.Contains("小时"))
            {
                sumSecond += int.Parse(timeStr.Substring(0, timeStr.IndexOf("小时"))) * 60 * 60;
                minIndex = timeStr.IndexOf("小时") + 2;
            }
            if (timeStr.Contains("分"))
            {
                sumSecond += int.Parse(timeStr.Substring(minIndex, timeStr.IndexOf("分") - minIndex)) * 60;
                secIndex = timeStr.IndexOf("分") + 2;
            }
            if (timeStr.Contains("秒"))
            {
                sumSecond += int.Parse(timeStr.Substring(secIndex, timeStr.IndexOf("秒") - secIndex));
            }
            return sumSecond;
        }
        #endregion

        ///// <summary>
        ///// 获取最终url，解析id from mall辅助方法
        ///// </summary>
        ///// <param name="originUrl">从

        /////vendor获取到的url</param>
        ///// <returns>最终url</returns>
        //public static string Parse(string originUrl)
        //{
        //    var url = string.Empty;
        //    using (var waiter = new WebBrowserWaiter.WebBrowserWaiter())
        //    {
        //        waiter.Await(wb =>
        //              {
        //                  var isCompleted = false;
        //                  var waitTime = 0;


        //                  const int waitTimeMax = 50;
        //                  wb.ScriptErrorsSuppressed = true;


        //                  wb.Navigate(originUrl);
        //                  wb.Navigating += (s, e) =>
        //                  {


        //                      Interlocked.Exchange(ref waitTime, 0);
        //                      isCompleted = false;
        //                  };


        //                  wb.DocumentCompleted += (s, e) =>
        //                                      {
        //                                          var browser = (s as WebBrowser);


        //                                          if (browser == null)
        //                                          {
        //                                              isCompleted = true;


        //                                              return;
        //                                          }
        //                                          if (browser.ReadyState == WebBrowserReadyState.Loaded ||


        //                               browser.ReadyState == WebBrowserReadyState.Complete)
        //                                              isCompleted = true;


        //                                      };
        //                  while (true)
        //                  {
        //                      if (isCompleted)
        //                      {


        //                          Interlocked.Exchange(ref waitTime, 0); //防止下面等待时跳出循环



        //                          System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(6)); //等一会儿，防止有js跳转没有检测到
        //                          if (isCompleted)
        //                          {
        //                              url = wb.Url.AbsoluteUri;
        //                              break;
        //                          }
        //                      }
        //                      //最多等waitTimeMax*1秒


        //                      System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(1));
        //                      Interlocked.Increment(ref waitTime);
        //                      if (Interlocked.CompareExchange(ref waitTime, waitTimeMax, waitTimeMax) >= waitTimeMax)
        //                      {
        //                          break; //防止内存溢出
        //                      }
        //                  }


        //              });
        //    }
        //    return url;
        //}
        /// <summary>
        /// 根据平台网站获取商品链接 获取商品源网站url 没得比
        /// </summary>
        /// <param name="url"></param>
        public static string GetTrueUrlFromMeidebi(string url, ref string productUrl)
        {
            //    string getIP = "http://vxer.daili666api.com/ip/?tid=555528787440920&num=3&foreign=none";
            //  var ips = GetProxyByHttpHelper(getIP);
            //   var iplist = Regex.Replace(ips, @"\s+", "|").Split('|').ToList();
            //for (int index = 0; index < iplist.Count; index++)
            //{
            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Timeout = 3000;//5秒超时
                myRequest.AllowAutoRedirect = false;
                //var proxylist = iplist[index].Split(':').ToList();
                //WebProxy proxyObject = new WebProxy(proxylist[0], Convert.ToInt32(proxylist[1]));
                //myRequest.Proxy = proxyObject;

                myRequest.UserAgent = "Mozilla/5.0 (compatible;Windows NT 6.1; WOW64;Trident/6.0;MSIE 9.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.27 Safari/537.36";
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                string redirectUrl = myResponse.GetResponseHeader("Location");
                // string fff = System.Web.HttpUtility.UrlDecode(redirectUrl, System.Text.Encoding.UTF8);
                if (redirectUrl == "")
                {
                    //continue;
                    //productUrl = url;
                    return "";
                }
                //京东 url 含有http://union.click.jd.com 时  增加尾巴转换转换
                string urlNil = "&a=fCg9UgoiAwwHO1BcXkQYFFlhcnx8elBdSVgzVRBSUll%2bAQAPDSwjLw%3d%3d&refer=norefer";
                if (redirectUrl.Contains("union.click.jd.com"))
                {
                    string clickUrl = "http://union.click.jd.com/jda?" + redirectUrl.Split('?').ToList()[1] + urlNil;
                    //再次请求http
                    myRequest = (HttpWebRequest)WebRequest.Create(clickUrl);
                    myRequest.Timeout = 5000;//5秒超时
                    myRequest.AllowAutoRedirect = false;
                    myRequest.UserAgent = "Mozilla/5.0 (compatible;Windows NT 6.1; WOW64;Trident/6.0;MSIE 9.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.27 Safari/537.36";
                    myResponse = (HttpWebResponse)myRequest.GetResponse();
                    redirectUrl = myResponse.GetResponseHeader("Location");
                    if (redirectUrl == "")
                    {
                        productUrl = url;
                        return "";
                    }
                }
                string sourceUrl = MallsEnumClass.GetSourceUrl(redirectUrl, ref productUrl);
                myResponse.Close();

                return sourceUrl;

            }
            catch (Exception e)
            {
                return "";
            }
            //  }
            return "";
        }
        /// <summary>
        /// 根据平台网站获取商品链接 获取商品源网站url
        /// </summary>
        /// <param name="url"></param>
        public static string GetTrueUrl(string url, ref string productUrl)
        {
            try
            {
                // string dd = GetWebHTML(url);
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Timeout = 5000;//5秒超时
                myRequest.AllowAutoRedirect = false;
                myRequest.UserAgent = "Mozilla/5.0 (compatible;Windows NT 6.1; WOW64;Trident/6.0;MSIE 9.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.27 Safari/537.36";
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                string redirectUrl = myResponse.GetResponseHeader("Location");
                // string fff = System.Web.HttpUtility.UrlDecode(redirectUrl, System.Text.Encoding.UTF8);
                if (redirectUrl == "")
                {
                    productUrl = url;
                    return "";
                }
                //京东 url 含有http://union.click.jd.com 时  增加尾巴转换转换
                string urlNil = "&a=fCg9UgoiAwwHO1BcXkQYFFlhcnx8elBdSVgzVRBSUll%2bAQAPDSwjLw%3d%3d&refer=norefer";
                if (redirectUrl.Contains("union.click.jd.com"))
                {
                    string clickUrl = "http://union.click.jd.com/jda?" + redirectUrl.Split('?').ToList()[1] + urlNil;
                    //再次请求http
                    myRequest = (HttpWebRequest)WebRequest.Create(clickUrl);
                    myRequest.Timeout = 5000;//5秒超时
                    myRequest.AllowAutoRedirect = false;
                    myRequest.UserAgent = "Mozilla/5.0 (compatible;Windows NT 6.1; WOW64;Trident/6.0;MSIE 9.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.27 Safari/537.36";
                    myResponse = (HttpWebResponse)myRequest.GetResponse();
                    redirectUrl = myResponse.GetResponseHeader("Location");

                    if (redirectUrl == "")
                    {
                        productUrl = url;
                        return "";
                    }
                }
                string sourceUrl = MallsEnumClass.GetSourceUrl(redirectUrl, ref productUrl);
                myResponse.Close();
                return sourceUrl;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        /// <summary>
        /// 根据正则和需要匹配的内容 找出想要的 信息
        /// </summary>
        /// <param name="regexStr"></param>
        /// <param name="htmlText"></param>
        /// 按索引取值
        /// <returns></returns>
        public static List<string> GetInfoByRegex(string regexStr, string htmlText, int index)
        {
            try
            {
                List<string> infoList = new List<string>();
                Regex regResource = new Regex(regexStr, RegexOptions.IgnoreCase);
                MatchCollection mResource = regResource.Matches(htmlText);

                foreach (Match itemhtmlText in mResource)
                {
                    infoList.Add(itemhtmlText.Groups[index].ToString());
                    if (infoList.Count == 18)
                    {
                        break;
                    }
                }
                return infoList;
            }
            catch (Exception ex)
            {
                WriteLog("GetInfoByRegex:" + ex.Message.ToString());
                return null;
            } 
        }
        /// <summary>
        /// 获取标签a中的链接 和内容
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List<string> GetInfoFroma(string str)
        {
            List<string> list = new List<string>();
            Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""]?)(?<url>[^'""\s>]+)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>");
            MatchCollection mc = reg.Matches(str);
            foreach (Match m in mc)
            {
                list.Add(m.Groups["url"].Value);
                 list.Add(m.Groups["text"].Value);
            }
            return list;
        }
        /// <summary>
        /// 写错误日志信息
        /// </summary>
        /// <param name="txt"></param>
        public static void WriteLog(string txt)
        {
            try
            {
                string path = Application.StartupPath + @"\log\" + DateTime.Now.ToString("yyyy-MM-dd") + @"\";

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);

                }
                path += DateTime.Now.ToString("yyyyMMdd") + "-" + DateTime.Now.ToString("HH") + ".txt";

                if (!File.Exists(path))
                {
                    using (FileStream fscreate = new FileStream(path, FileMode.Create))
                    {
                        fscreate.Close();
                    }
                }
                using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                    {
                        sw.Write(DateTime.Now.ToString("HH:mm:ss") + " " + txt + "\r\n");
                        sw.Close();
                    }
                    fs.Close();
                }
            }

            catch (Exception ex)
            {
                WriteLog("程序发生异常（WriteLog）。详情：" + ex.Message);

            }

        }
        /// <summary>
        /// 根据网站跳转url获取商品源url
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetUrlByCurrentUrl(string url, ref string productUrl)
        {
            try
            {
                string sourceUrl = "";
                string html = GetHtmlByHttpHelper(url);
                string headRegex = @"<head>[\s\S]*?</head>";
                var content = GetInfoByRegex(headRegex, html, 0);
                if (content == null || content.Count < 1)
                {
                    //如果没有找到内容则根据301跳转尝试获取url 如果为空 则为空
                    string urlStr = GetTrueUrl(url, ref productUrl);
                    return urlStr;
                }
                string urlRegex = @"content=[""|']([^""|']*)[""|']";
                var urlList = GetInfoByRegex(urlRegex, content[0], 1);
                if (urlList == null || urlList.Count < 2)
                {
                    //如果没有找到内容则根据301跳转尝试获取url 如果为空 则为空
                    string urlStr = GetTrueUrl(url, ref productUrl);
                    return urlStr;
                }

                int startIndex = urlList[1].IndexOf("http");
                if (startIndex != -1)
                {
                    sourceUrl = urlList[1].Substring(startIndex, urlList[1].Length - startIndex);
                    string result = "";
                    if (sourceUrl.Contains("ju.suning.com"))
                    {
                        result = MallsEnumClass.GetSourceUrl(sourceUrl.Split('?').ToList()[0], ref productUrl);
                    }
                    else
                    {
                        result = MallsEnumClass.GetSourceUrl(sourceUrl, ref productUrl);
                    }
                    return result;
                }
                productUrl = url;
                return "";
            }
            catch (Exception ex)
            {
                //错误日志  
                CustomMethod.WriteLog("GetUrlByCurrentUrl 302跳转获取url：" + url + " :" + ex.Message.ToString());
                return "";
            }
        }
        /// <summary>
        /// 获取js eval加密了的url  结果为解密后
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetEvalUrl(string url, ref string productUrl, typeOfWeb webType)
        {
            try
            {
                string sourceUrl = "";
                string html = GetHtmlByHttpHelper(url);
                int startIndex = html.IndexOf("eval");
                int endIndex = html.IndexOf("</script>");
                if (startIndex == -1)
                {
                    sourceUrl = MallsEnumClass.GetSourceUrl(url, ref productUrl);
                    return url;
                }
                else if (endIndex > startIndex)
                {
                    string evalStr = html.Substring(startIndex, endIndex - startIndex);
                    string result = Eval(evalStr);
                    int herfStartIndex = 0;// result.IndexOf("smzdmhref='");
                    int herfEndIndex = 0; // result.IndexOf("';ga");
                    if (webType == typeOfWeb.SMZDMFX)
                    {
                        herfStartIndex = result.IndexOf("smzdmhref1='");
                        herfEndIndex = result.IndexOf("';ga");
                        if (herfStartIndex < 0)
                        {
                            herfStartIndex = result.IndexOf("http");
                            herfEndIndex = result.IndexOf("';var");
                            if (herfEndIndex > 0)
                            {
                                string currentUrl = result.Substring(herfStartIndex, herfEndIndex - herfStartIndex);
                                sourceUrl = MallsEnumClass.GetSourceUrl(currentUrl, ref productUrl);
                                return sourceUrl;
                            }
                            else
                            {
                                return "";
                            }
                        }
                        else if (herfEndIndex > herfStartIndex + 11)
                        {
                            string currentUrl = result.Substring(herfStartIndex + 12, herfEndIndex - herfStartIndex - 12);
                            //int end = currentUrl.LastIndexOf("&to=");
                            //int cout = 4;
                            //if(end==-1)
                            //{
                            //    end = currentUrl.LastIndexOf("smzdmhref1=");
                            //    cout = 11;
                            //}
                            string lastulr = currentUrl; //currentUrl.Substring(end + cout, currentUrl.Length - end - cout);
                            sourceUrl = MallsEnumClass.GetSourceUrl(lastulr, ref productUrl);
                            return sourceUrl;
                        }
                    }
                    else if (webType == typeOfWeb.HotProduct)
                    {
                        herfStartIndex = result.IndexOf("http");
                        herfEndIndex = result.IndexOf("';");
                        if (herfEndIndex > herfStartIndex)
                        {
                            string currentUrl = result.Substring(herfStartIndex, herfEndIndex - herfStartIndex);
                            sourceUrl = MallsEnumClass.GetSourceUrl(currentUrl, ref productUrl);
                            return sourceUrl;
                        }
                    }
                    return result;
                }
                else
                {
                    return url;
                }
            }
            catch (Exception ex)
            {
                WriteLog("GetEvalUrl:" + url + ":" + ex.Message.ToString());
                return "";
            }

        }
        /// <summary>
        /// js eval解密方法
        /// </summary>
        /// <param name="stringjs"></param>
        /// <returns></returns>
        private static string Eval(string stringjs)
        {
            string MyJs = "function Eval(code){code2=code.replace(/^eval/,'');return eval(code2);}";
            object[] _params = new object[1];
            _params[0] = stringjs;
            MSScriptControl.ScriptControl js = new MSScriptControl.ScriptControl();
            js.Language = "javascript";
            js.AddCode(MyJs);
            string result = js.Run("Eval", _params);//NET4.0以下为js.Run("Eval",ref _params).ToString();
            return result;
        }
        public static string GetImgUrl(string content, string webName)
        {
            //图片链接
            string srcRegex = @"src=[""|']([^""|']*)[""|']";
            var srcImg = GetInfoByRegex(srcRegex, content, 1);
            if (srcImg.Count > 0)
            {
                if (srcImg[0].Contains(".gif"))
                {
                    srcRegex = @"#src=[""|']([^""|']*)[""|']";
                    srcImg = GetInfoByRegex(srcRegex, content, 1);
                }
            }
            else if (srcImg == null || srcImg.Count < 1 || srcImg[0] == "")
            {
                string msg = webName + "：没有获取到图片链接";
                WriteLog(msg);
                return "";
            }
            return srcImg[0];

        }
        public static void UpdateData()
        {
            jutehui.BLL.Product bll = new jutehui.BLL.Product();
            Product product = new Product();
            bll.Update(product);
        }
        /// <summary>
        /// 返回没有重复的商品数据
        /// </summary>
        /// <param name="plist"></param>
        /// <returns></returns>
        public static List<Product> GetDistinctProdcuts(List<Product> plist)
        {
            List<Product> list = new List<Product>();
            if (plist != null && plist.Count > 0)
            {
                for (int index = 0; index < plist.Count; index++)
                {
                    if (list == null || list.Count == 0)
                    {
                        list.Add(plist[index]);
                    }
                    else
                    {
                        var item = list.Where(o => o.Memo == plist[index].Memo);
                        if (item == null || item.ToList().Count < 1)
                        {
                            list.Add(plist[index]);
                        }
                    }
                }
            }
            return list;
        }
        /// <summary>
        /// 将图片保存到本地
        /// </summary>
        /// <param name="webName">站点</param>
        /// <param name="url">图片地址</param>
        /// <returns></returns>
        public static string SavePic(string webName, string url)
        {
            string name = Guid.NewGuid().ToString();
            try
            {
                Image img = Image.FromStream(WebRequest.Create(url).GetResponse().GetResponseStream());
                img.Save(@"C:\publish\img\" +webName+@"\"+ name + ".png");
            }
            catch (Exception ex)
            {
                return url;
            }
            //http://www.maytao.cn/Images/
            string res = "/img/" + webName + "/" + name + ".png";
            return res;
        }

        /// <summary>
        /// 获取商品信息
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="rule"></param>
        /// <returns></returns>
        public static string GetValue(HtmlAgilityPack.HtmlDocument doc, string rule)
        {
            var item = doc.DocumentNode.SelectNodes(rule);
            if (item != null && item.Count > 0)
            {
                return item[0].InnerText;
            }
            return "";
        }
        /// <summary>
        ///根据规则获取属性值
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <returns></returns>
        public static List<string> GetAttr(HtmlAgilityPack.HtmlDocument doc,string htmlContent, string rule, string attr)
        {
            List<string> list = new List<string>();
            doc.LoadHtml(htmlContent);
            var conllectin = doc.DocumentNode.SelectNodes(rule);//".//a[@href]"
            if (conllectin != null)
            {
                for (int index = 0; index < conllectin.Count; index++)
                {
                    HtmlAttribute att = conllectin[index].Attributes[attr];
                    list.Add(att.Value);
                }
            }
            return list;
        }
        /// <summary>
        /// 网站enum类型 对应截取位置
        /// </summary>
        public enum typeOfWeb
        {
            SMZDMFX = 1,
            HotProduct = 2
        }
    }
}
